package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.bill.*;
import com.canaan.business.service.bill.ISalesRefundBillService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.SalesRefundBillDetailMapper;
import com.canaan.business.service.bill.ISalesRefundBillDetailService;

/**
 * 销售退货单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-31
 */
@Service
public class SalesRefundBillDetailServiceImpl implements ISalesRefundBillDetailService {
    @Resource
    private SalesRefundBillDetailMapper salesRefundBillDetailMapper;
    @Resource
    private ISalesRefundBillService salesRefundBillService;

    /**
     * 查询销售退货单明细
     *
     * @param id 销售退货单明细主键
     * @return 销售退货单明细
     */
    @Override
    public SalesRefundBillDetail selectSalesRefundBillDetailById(Long id) {
        return salesRefundBillDetailMapper.selectSalesRefundBillDetailById(id);
    }

    /**
     * 查询销售退货单明细列表
     *
     * @param salesRefundBillDetail 销售退货单明细
     * @return 销售退货单明细
     */
    @Override
    public List<SalesRefundBillDetail> selectSalesRefundBillDetailList(SalesRefundBillDetail salesRefundBillDetail) {
        return salesRefundBillDetailMapper.selectSalesRefundBillDetailList(salesRefundBillDetail);
    }

    /**
     * 新增销售退货单明细
     *
     * @param salesRefundBillDetail 销售退货单明细
     * @return 结果
     */
    @Override
    public int insertSalesRefundBillDetail(SalesRefundBillDetail salesRefundBillDetail) {
        if(StringUtils.isBlank(salesRefundBillDetail.getNo())){
            salesRefundBillDetail.setNo(TableNoUtil.getNo(SalesRefundBillDetail.class));
        }
        salesRefundBillDetail.setCreateBy(SecurityUtils.getUsername());
        salesRefundBillDetail.setCreateTime(DateUtils.getNowDate());
        int i = salesRefundBillDetailMapper.insertSalesRefundBillDetail(salesRefundBillDetail);
        refreshTotalQty(salesRefundBillDetail.getBillNo());
        return i;
    }

    private void refreshTotalQty(String billNo){
        SalesRefundBillDetail detail = new SalesRefundBillDetail();
        detail.setBillNo(billNo);
        List<SalesRefundBillDetail> details = this.selectSalesRefundBillDetailList(detail);
        SalesRefundBill bill = salesRefundBillService.selectSalesRefundBillByNo(billNo);
        bill.setTotalQty(details.stream().filter(e -> e.getQty() != null).mapToLong(e -> e.getQty()).sum());
        salesRefundBillService.updateSalesRefundBill(bill);
    }

    /**
     * 批量新增销售退货单明细
     *
     * @param salesRefundBillDetailList 销售退货单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertSalesRefundBillDetail(List<SalesRefundBillDetail> salesRefundBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SalesRefundBillDetail salesRefundBillDetail : salesRefundBillDetailList){
            if(StringUtils.isBlank(salesRefundBillDetail.getNo())){
                salesRefundBillDetail.setNo(TableNoUtil.getNo(SalesRefundBillDetail.class));
            }
            salesRefundBillDetail.setCreateBy(username);
            salesRefundBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<salesRefundBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > salesRefundBillDetailList.size()) {
                endIndex = salesRefundBillDetailList.size();
            }
            insertNum = insertNum + salesRefundBillDetailMapper.batchInsertSalesRefundBillDetail(salesRefundBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改销售退货单明细
     *
     * @param salesRefundBillDetail 销售退货单明细
     * @return 结果
     */
    @Override
    public int updateSalesRefundBillDetail(SalesRefundBillDetail salesRefundBillDetail) {
        salesRefundBillDetail.setUpdateBy(SecurityUtils.getUsername());
        salesRefundBillDetail.setUpdateTime(DateUtils.getNowDate());
        salesRefundBillDetailMapper.updateSalesRefundBillDetail(salesRefundBillDetail);
        refreshTotalQty(salesRefundBillDetail.getBillNo());
        return salesRefundBillDetailMapper.updateSalesRefundBillDetail(salesRefundBillDetail);
    }

    /**
     * 批量修改销售退货单明细
     *
     * @param salesRefundBillDetailList 销售退货单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateSalesRefundBillDetail(List<SalesRefundBillDetail> salesRefundBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SalesRefundBillDetail salesRefundBillDetail : salesRefundBillDetailList){
            salesRefundBillDetail.setUpdateBy(username);
            salesRefundBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<salesRefundBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > salesRefundBillDetailList.size()) {
                endIndex = salesRefundBillDetailList.size();
            }
            updateNum = updateNum + salesRefundBillDetailMapper.batchUpdateSalesRefundBillDetail(salesRefundBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除销售退货单明细
     *
     * @param ids 需要删除的销售退货单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillDetailByIds(Long[] ids) {
        int i = salesRefundBillDetailMapper.deleteSalesRefundBillDetailByIds(ids);
        SalesRefundBillDetail salesRefundBillDetail = this.selectSalesRefundBillDetailById(ids[0]);
        refreshTotalQty(salesRefundBillDetail.getBillNo());
        return i;
    }

    /**
     * 删除销售退货单明细信息
     *
     * @param id 销售退货单明细主键
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillDetailById(Long id) {
        return salesRefundBillDetailMapper.deleteSalesRefundBillDetailById(id);
    }

    /**
     * 查询销售退货单明细
     *
     * @param no 编号
     * @return 销售退货单明细
     */
    @Override
    public SalesRefundBillDetail selectSalesRefundBillDetailByNo(String no) {
        return salesRefundBillDetailMapper.selectSalesRefundBillDetailByNo(no);
    }

    /**
     * 批量删除销售退货单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillDetailByNos(String[] nos) {
        return salesRefundBillDetailMapper.deleteSalesRefundBillDetailByNos(nos);
    }

    /**
     * 删除销售退货单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillDetailByNo(String no) {
        return salesRefundBillDetailMapper.deleteSalesRefundBillDetailByNo(no);
    }
}
